#include <QApplication>
#include <QDateTime>

#include <qdebug.h>

const QString getTimeAgo(int timestamp)
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    int currentTimestamp = currentDateTime.toTime_t();

    bool isFuture = false;
    int secDiff = 0;
    if ( timestamp > currentTimestamp )//future
    {
        isFuture = true;
        secDiff = timestamp - currentTimestamp;
    }
    else if ( timestamp < currentTimestamp )//past
    {
        isFuture = false;
        secDiff = currentTimestamp - timestamp;
    }
    else//the same
    {
        return QString(QObject::tr("Just now"));
    }

    QString ret;

    if ( secDiff > 365*24*60*60 )//longer than 1 year
    {
        int years = secDiff / (365*24*60*60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ years in the future").arg(years);
        }
        else
        {
            ret = QObject::tr("%1+ years ago").arg(years);
        }
    }
    else if ( secDiff <= 365*24*60*60 && 30*24*60*60 < secDiff )//shorter than 1 year and longer than 1 month
    {
        int months = secDiff / (30*24*60*60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ months in the future").arg(months);
        }
        else
        {
            ret = QObject::tr("%1+ months ago").arg(months);
        }
    }
    else if ( secDiff <= 30*24*60*60 && 24*60*60 < secDiff )//shorter than 1 month and longer than 1 day
    {
        int days = secDiff / (24*60*60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ days in the future").arg(days);
        }
        else
        {
            ret = QObject::tr("%1+ days ago").arg(days);
        }
    }
    else if ( secDiff <= 24*60*60 && 60*60 < secDiff )//shorter than 1 day and longer than 1 hour
    {
        int hours = secDiff / (60*60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ hours in the future").arg(hours);
        }
        else
        {
            ret = QObject::tr("%1+ hours ago").arg(hours);
        }
    }
    else if ( secDiff <= 60*60 && 60 < secDiff )//shorter than 1 hour and longer than 1 minute
    {
        int minutes = secDiff / (60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ minutes in the future").arg(minutes);
        }
        else
        {
            ret = QObject::tr("%1+ minutes ago").arg(minutes);
        }
    }
    else if ( secDiff <= 60 )//shorter than 1 minute
    {
        if ( isFuture )
        {
            ret = QObject::tr("less than 1 minute in the future");
        }
        else
        {
            ret = QObject::tr("less than 1 minute ago");
        }
    }
    return ret;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString createdAt = "2024-03-29T08:26:50+00:00";
    //    "created_at": "2024-02-15T08:43:43+08:00",
    // auto dt = QDateTime::fromString(createdAt, "yyyy-MM-ddTHH:mm:ss+zzz");
    auto dt = QDateTime::fromString(createdAt, Qt::ISODateWithMs);
    qDebug() << dt;

    qDebug() << getTimeAgo(dt.toTime_t());
    qDebug() << getTimeAgo(QDateTime::currentDateTime().toTime_t());
    return 0;
}
